<template>
  <div class="forgotContainer">
    <div class="forgotWrapper">
      <ToBack text="找回密码" />
      <div class="btnBox">
        <div
          v-for="(item, index) in BtnMenuList"
          :key="index"
          @click="handleBtnClick(index)"
          :class="['btn', currBtn === index ? 'currBtn' : '']"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="mainBox">
        <el-form :model="form" ref="formRef" label-width="80px">
          <el-form-item label="用户名">
            <div class="input-with-desc">
              <el-input v-model="form.username" placeholder="请输入" />
              <span class="description">请输入用户名</span>
            </div>
          </el-form-item>

          <el-form-item label="邮箱地址">
            <div class="input-with-desc">
              <el-input v-model="form.email" placeholder="请输入" />
              <span class="description">请输入邮箱地址</span>
            </div>
          </el-form-item>

          <el-form-item label="新密码">
            <div class="input-with-desc">
              <el-input
                v-model="form.newPassword"
                type="password"
                placeholder="请输入"
              />
              <span class="description">至少1个长度,最多20个长度</span>
            </div>
          </el-form-item>

          <el-form-item label="确认密码">
            <div class="input-with-desc">
              <el-input
                v-model="form.confirmPassword"
                type="password"
                placeholder="请输入"
              />
              <span class="description">至少1个长度,最多20个长度</span>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
  <div class="footerTime">
    <p>当前时间(UTC)：{{ utcTime }}</p>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import ToBack from "./components/ToBack.vue";
import { useTime } from "@/utils/tools/useTime";
import { meForgetPasswordApi } from "@/api/user";

// 表单引用
const formRef = ref(null);
// 表单数据
const form = reactive({
  username: "",
  email: "",
  newPassword: "",
  confirmPassword: "",
});

const BtnMenuList = ref([{ name: "提交" }, { name: "重置" }]);
const { utcTime } = useTime();
const currBtn = ref(0);

const handleBtnClick = (index) => {
  currBtn.value = index;
  if (index === 0) {
    submitForm();
  } else if (index === 1) {
    resetForm();
  }
};

const submitForm = async () => {
  if (form.newPassword !== form.confirmPassword) {
    ElMessage.error("两次输入的密码不一致，请重新输入");
    return;
  }

  if (!form.username || !form.email || !form.newPassword) {
    ElMessage.warning("用户名、邮箱地址、新密码为必填项");
    return;
  }

  const paramsObj = {
    newPassword: form.newPassword,
    userMail: form.email,
    userName: form.username,
  };
  const { code } = await meForgetPasswordApi(paramsObj);
  if (code === 200) {
    ElMessage.success("找回成功");
  }
};

const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields();
  }
  form.username = "";
  form.email = "";
  form.newPassword = "";
  form.confirmPassword = "";
};
</script>

<style scoped lang="scss">
.forgotContainer {
  box-sizing: border-box;
  padding: 12px;
  overflow: hidden;
  width: 100vw;
  height: calc(100vh - 28px);
  .forgotWrapper {
    position: relative;
    box-sizing: border-box;
    width: 100%;
    height: 100%;
    margin: 0 auto;
    border-radius: 12px;
    border: 2px solid #003b7a;
    .btnBox {
      position: absolute;
      width: 180px;
      top: 14px;
      right: 8px;
      font-size: 14px;
    }

    .btn {
      cursor: pointer;
      float: left;
      margin-left: 12px;
      color: #ffffff;
      width: 64px;
      height: 32px;
      padding: 5px 8px;
      border-radius: 4px;
      border: 1px solid #003b7a;
    }
    .currBtn {
      background: #00afff;
    }

    ::v-deep .el-form-item__label {
      color: #ffffff;
      font-size: 14px;
      line-height: 40px;
      justify-content: flex-start;
    }

    ::v-deep .el-input__wrapper {
      width: 400px;
      padding: 4px 4px 4px 8px;
      border-radius: 4px;
      border: 1px solid #3c4660;
      box-shadow: none;
      background-color: transparent;
      .el-input__inner {
        font-size: 16px;
        color: #ffffff !important;
      }
    }
    .mainBox {
      margin-top: 60px;
      margin-left: 115px;
      clear: both;
      .el-form-item {
        margin-bottom: 10px;
      }
    }
  }
}
.input-with-desc {
  display: flex;
  align-items: center;
}

.description {
  margin-left: 12px;
  color: #c5d6e6;
  font-size: 12px;
  white-space: nowrap;
}
.footerTime {
  width: 100%;
  height: 28px;
  background-color: #050d24;
  p {
    color: #fff;
    text-align: right;
    font-size: 12px;
    line-height: 20px;
    padding-right: 16px;
  }
}
.forgotContainer .forgotWrapper .mainBox {
  ::v-deep .el-input__wrapper {
    height: 28px !important;
    border: 1px solid #3c4660 !important;
  }
  ::v-deep .el-input__inner {
    font-size: 12px !important;
  }
  ::v-deep .el-input--password .el-input__wrapper {
    height: 28px !important;
  }
}
</style>
